﻿@model RMS.Models.Company

@{
    ViewBag.Title = "Details";
    int topicNumber = 1;
    RMS.Models.User user = null;
    if(ViewData["user"] != null)
    {
        user = ViewData["user"] as RMS.Models.User;
    }
}

<h2>Details</h2>

<fieldset>
    <legend>Company Profile</legend>

    <div class="display-label">Name</div>
    <div class="display-field">@Model.Name</div>
    
    <div class="display-label">Share Information to Public</div>
    <div class="display-field">@Model.IsShared</div>

    <div class="display-label">Problem Group's Cost Limit</div>
    <br />
    <table>
    @foreach (var limit in Model.CompanyProblemLimits)
    {
        <tr>
        <th>@limit.ProblemGroup.Name<th>
        <td>@limit.Limit.ToString("n2")</td>
        @if (user.Company.FouderUserId == user.Id)
        {
            <th>
                @using (Html.BeginForm("EditCompanyLimit", "Company", FormMethod.Post))
                {
                    @Html.Hidden("limitId", limit.Id)
                    @Html.TextBox("limit", limit.Limit, new { @class = "small" })
                    <input type="submit" value="edit" />
                }
            </th>
        }

        </tr>
    }
    </table>
    

</fieldset>

    <table>
        <tr>
            <th>Problem</th>
            <th>Impact</th>
            <th>Probability</th>
            <th>Discrimination</th>
            <th>Risk Score</th>
        </tr>
        @foreach (var item in Model.CompaniesProblems.AsEnumerable())
        {
            <tr>
                <td>@Html.ActionLink(item.Problem.Name, "Details", "Problem", new { id = item.ProblemId }, null)</td>
                @if (item.Problem.ProblemScores.Any(x => x.UserId == Model.FouderUserId))
                {
                    RMS.Models.ProblemScore score = item.Problem.ProblemScores.FirstOrDefault(x => x.UserId == Model.FouderUserId);
                    <td>@score.ImpactScore</td>
                    <td>@score.ProbabilityScore</td>
                    <td>@score.DiscriminationScore</td>
                    <td>@score.RiskScore.ToString("n2")</td>
                }
                else
                {
                    <td colspan="4">Not Yet Scored</td>
                }
                
            </tr>
        }
    </table>
    <br />
<fieldset>
    <legend>Comments</legend>

    @foreach (var topic in Model.CompanyTopics)
    {
        <br />  
        <table>
            <tr>
                <th>@(topicNumber++)</th>
                <th>Topic: @topic.Title</th>
                <th>User: @topic.User.UserName</th>
                <th>Date: @topic.Date</th>
                <th>
                    @if (Request.IsAuthenticated)
                    {
                        if (topic.CompanyTopicLikes.Any(x => x.UserId == user.Id))
                        {
                             @Html.ActionLink("Unlike", "CompanyTopicUnlike", "Topic", new { topicId = topic.Id, userId = user.Id }, null)
                        }
                        else
                        {
                            @Html.ActionLink("Like", "CompanyTopicLike", "Topic", new { topicId = topic.Id, userId = user.Id }, null)
                        }
                    }
                    else
                    {

                    }
                </th>
            </tr>
            <tr>
                <th></th>
                <th colspan="4">Like(s): 
                @foreach (var like in topic.CompanyTopicLikes)
                {
                    @Html.Encode(like.User.UserName)<text>&nbsp;</text>
                }
                </th>
            </tr>
            <tr>
                <th></th>
                <th colspan="4" style="background-color: #F2F2F2;">@topic.Details</th>
            </tr>
            @{int replyNumber = 1;
            }

            @foreach (var reply in topic.CompanyTopicReplies)
            {
                <tr>
                    <td>@(replyNumber++)</td>
                    <td>@reply.Details</td>
                    <td>User: @reply.User.UserName</td>
                    <td>Date: @reply.Date</td>
                    <td>
                    @if (Request.IsAuthenticated)
                    {
                        if (reply.CompanyTopicReplyLikes.Any(x => x.UserId == user.Id))
                        {
                             @Html.ActionLink("Unlike", "CompanyTopicReplyUnlike", "Topic", new { replyId = reply.Id, userId = user.Id }, null)
                        }
                        else
                        {
                            @Html.ActionLink("Like", "CompanyTopicReplyLike", "Topic", new { replyId = reply.Id, userId = user.Id }, null)
                        }
                    }
                    else
                    {

                    }
                    </td>
                </tr>
                <tr>
                    <td colspan="5">Like(s): 
                    @foreach (var like in reply.CompanyTopicReplyLikes)
                    {
                        @(Html.Encode(like.User.UserName))<text>&nbsp;</text> 
                    }
                    </td>
                </tr>
                
            }
            @if (Request.IsAuthenticated)
            {
                <tr>
                    @using (Html.BeginForm("AddCompanyTopicReply", "Topic", FormMethod.Post))
                    {
                    <td colspan="5">
                    Reply: 
                    @Html.TextBox("details")
                    @Html.Hidden("userId", user.Id)
                    @Html.Hidden("topicId", topic.Id)
                    <input type="submit" value="submit" /> 
                    
                    </td>
                    }
                </tr>
            }
        </table>
    }
        @if (Request.IsAuthenticated)
        {
            <br />
            <table>
                @using (Html.BeginForm("AddCompanyTopic", "Topic", FormMethod.Post))
                {
                    <tr>
                        <th>
                            Title: 
                        </th>
                        <th>
                            @Html.TextBox("title", "")
                            @Html.Hidden("userId", user.Id)
                            @Html.Hidden("companyId", Model.Id)
                        </th>
                    </tr>
                    <tr>
                        <th>
                            Details:
                        </th>
                        <th>
                            @Html.TextArea("details")
                        </th>
                    </tr>
                    <tr>
                        <th></th>
                        <th>
                            <input type="submit" value="submit" />
                        </th>
                    </tr>
                }
            </table>
        }
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
    @Html.ActionLink("Back to List", "Index")
</p>

